<template>
	<view>
		<view class="uni-common-mt">
			<view class="uni-form-item uni-column">
				<input v-model="inputValue" focus="true" class="uni-input" placeholder="请输入内容" />
			</view>

			<view style="width:100%; margin-top:30upx;">
				<button @click="submit()">提交</button>
			</view>

		</view>
	</view>
</template>
<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';	
	export default {
		computed: {
			...mapState(['userInfo', 'hasLogin', 'loginProvider', 'openid'])
		},
		data() {
			return {
				inputValue: "",
				paraName: "",
			}
		},
		onLoad(e){
			this.paraName = e.paraName;
			this.inputValue = e.content;
		},
		methods: {
			...mapMutations(['login', 'logout', 'setOpenid', 'setProvider']),
			submit: function(e) {
				if (this.paraName == 'telephone') {
					var reg = /^[1][3,4,5,7,8][0-9]{9}$/;
					if (!reg.test(this.inputValue)) {
						uni.showToast({
							title: "手机号格式不正确",
							icon: "none"
						})
						return;
					}
				}
				if (this.paraName == 'email') {
					var reg = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
					if (!reg.test(this.inputValue)) {
						uni.showToast({
							title: "邮箱格式不正确",
							icon: "none"
						})
						return;
					}
				}
				this.saveUser(this.paraName, this.inputValue);
			},
			saveUser(sname,svalue){
				var data ={
					"username":this.userInfo.username
				}
				data[sname]=svalue;
				uni.request({
					url: this.serverUrl + 'user-api/editUser',
					header: {
						"content-type": "application/x-www-form-urlencoded; charset=UTF-8"
					},
					method: 'POST',
					data: data,
					success: (r) => {
						//console.log(r);
						if (r.data.code == '000000') {
							uni.navigateBack({
								delta: 1
							})
						} else {
							uni.showToast({
								title: r.errMsg
							});
						}
					},
					fail: (err) => {
						uni.showModal({
							content: err.errMsg,
							showCancel: false
						})
					}
				})
			},
		}
	}
</script>

<style>
	page {
		background: #F6F7F8;
	}

	view button {
		width: 92%;
		color: #fff;
		height: 80upx;
		font-size: 36upx;
		line-height: 80upx;
		background: #417dc9;
		border: 0;
	}
</style>
